package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.PlayerRelateViewLiveMapper;
import com.originskyseed.platform.user.model.PlayerRelateViewLive;
import com.originskyseed.platform.user.service.IPlayerRelateViewLiveService;

/**
 * 玩家观看直播信息表Service接口实现类（OSI_PLAYER_RELATE_VIEW_LIVE）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("playerRelateViewLiveService")
public class PlayerRelateViewLiveServiceImpl implements IPlayerRelateViewLiveService {

	@Resource
	private PlayerRelateViewLiveMapper playerRelateViewLiveMapper;


	@Resource
	private UserInfoMapper userInfoMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateViewLiveMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateViewLiveMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateViewLive record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateViewLiveMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateViewLive record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateViewLiveMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateViewLive record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateViewLiveMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateViewLive record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateViewLiveMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateViewLive selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateViewLiveMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateViewLive> selectList(PlayerRelateViewLive record) throws Exception {
		return this.playerRelateViewLiveMapper.selectList(record);
	}

	/**
	 * 查询记录
	 *
	 * @param liveId
	 * @return
	 * @throws Exception
	 */
	public PlayerRelateViewLive selectByLiveId(String liveId) throws Exception{
		return this.playerRelateViewLiveMapper.selectByLiveId(liveId);
	}

	/**
	 * 查询记录
	 *
	 * @param liveId
	 * @return
	 * @throws Exception
	 */
	public PlayerRelateViewLive selectByLiveId(String tenantCode,String liveId) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.playerRelateViewLiveMapper.selectByLiveId(liveId);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateViewLive> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateViewLive> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateViewLive> list = this.playerRelateViewLiveMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(List<PlayerRelateViewLive> list) throws Exception {
		return this.playerRelateViewLiveMapper.insertBatch(list);
	}

	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(List<PlayerRelateViewLive> list, List<PlayerRelateViewLive> updateList) throws Exception {
		if (list.size() > 0) {
			this.playerRelateViewLiveMapper.insertBatch(list);
		}
		for (PlayerRelateViewLive playerRelateViewLive : updateList) {
			this.playerRelateViewLiveMapper.updateByPrimaryKeySelective(playerRelateViewLive);
		}
		return 1;
	}


	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(String tenantCode,List<PlayerRelateViewLive> list, List<PlayerRelateViewLive> updateList) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return insertBatch(list,updateList);
	}

	/**
	 * 批量插入记录，月表
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertMonthBatch(List<PlayerRelateViewLive> list, List<PlayerRelateViewLive> updateList) throws Exception {
		if (list.size() > 0) {
			this.playerRelateViewLiveMapper.insertMonthBatch(list);
		}
		for (PlayerRelateViewLive playerRelateViewLive : updateList) {
			this.playerRelateViewLiveMapper.updateMonthByPrimaryKeySelective(playerRelateViewLive);
		}
		return 1;
	}


	/**
	 * 批量插入记录,月表
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertMonthBatch(String tenantCode,List<PlayerRelateViewLive> list, List<PlayerRelateViewLive> updateList) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return insertMonthBatch(list,updateList);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateViewLive> selectListRelAnchorByPage(Map<String, Object> params, BasePage<PlayerRelateViewLive> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateViewLive> list = this.playerRelateViewLiveMapper.selectListRelAnchorByPage(params);
		if(list!=null&&list.size()>0)
		{
           for(int i=0;i<list.size();i++)
		   {
			   PlayerRelateViewLive playerRelateViewLive=list.get(i);
			   String subChannelSubId=playerRelateViewLive.getChannelSubId();
			   if(subChannelSubId!=null&&StringUtils.isNotBlank(subChannelSubId)) {
				   UserInfo userInfo=userInfoMapper.selectByOtherAccount(subChannelSubId);
				   playerRelateViewLive.setBelongBusinessName(userInfo==null?"":userInfo.getName());
			   }
			   if(playerRelateViewLive.getEndTime()!=null&&playerRelateViewLive.getStartTime()!=null) {
			   	   Long viewingTime=(playerRelateViewLive.getEndTime().getTime()-playerRelateViewLive.getStartTime().getTime())/(1000*60);
				   playerRelateViewLive.setViewingTime(Integer.parseInt(viewingTime.toString()));
			   }
		   }
		}
		page.setResults(list);
		return page;
	}

	/**
	 * 查询观看人数
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public Integer selectWatchNumber(Map<String, Object> params) throws Exception {
		return this.playerRelateViewLiveMapper.selectWatchNumber(params);
	}
}